using System;
using System.Collections;
using Awaiter;
using YesPlayLove.Classes;
using jQueryApi;
using Nsb.Classes;
using Yoshi.Classes;
using Yoshi.Pages;

namespace YoshiFlowNewBee
{
    public class NewBeePage1 : NewBeePageBase
    {
        public NewBeePage1()
        {
            string nextPage = typeof(NewBeePage2).FullName;
            const string aliasName = "alias";
            const string contBtnName = "contBtn";
            const string questions = "Questions";

            string q1Name = "";
            Dictionary question1 = null;
            string q2Name = "";
            Dictionary question2 = null;

            new Await()
                .AddAw(LoadNamespaceCssAw)
                .AddAw(BasicBuildAw, NewBeeTheme.NewBeePage1Title)
                .AddDx(delegate { PageCluster.AddClass("TrupleCluster"); })
                .LoadJson(questions, "Scripts/Data/NewBeeQuestions.txt")
                .AddDx(
                    delegate
                    {
                        jQuery.FromHtml("<img width='300'/>")
                            .AppendTo(PageCluster)
                            .CSS(new Dictionary("position", "absolute", "top", -160, "left", 460))
                            .Attribute("src", "images/Common/PlayAlias1.png")
                            ;

                        //Actor slideShow = Viewit("https://docs.google.com/present/embed?id=dcxgm95h_820gm7svsdq&autoStart=true&loop=true&interval=7")
                        Viewit("https://docs.google.com/presentation/embed?id=1s48FxzELpi-Thl0smwECUEmIqoVE3Ydwe4W_RzUXSOQ&start=true&loop=true&delayms=7000")
                            .AppendTo(PageCluster)
                            .AddClass("SlideShow")
                            ;
                        //slideShow.Element.Plugin<IRot>().Rotate(0);

                        //
                        // Randomly choose questions
                        //
                        int ct = Await.FileDictionary(questions).Count;
                        int q1 = Math.Floor(Math.Random() * ct) + 1;
                        int q2 = Math.Floor(Math.Random() * ct) + 1;
                        while (q1 == q2)
                            q2 = Math.Floor(Math.Random() * ct) + 1;
                        question1 = Await.Vita(questions, q1.ToString());
                        q1Name = (string)question1["name"];
                        question1["hide"] = true;
                        question1["question"] = "Hi %alias%. " + question1["question"];

                        question2 = Await.Vita(questions, q2.ToString());
                        q2Name = (string)question2["name"];
                        question2["hide"] = true;
                    })
                .AddDx(
                    delegate
                    {
                        new Await()
                            .AddAw(PlayAliasAw, new Dictionary("Label", "Please choose your Play Alias", "Validate", false))
                            .AddAw(MultipleChoiceAw, question1)
                            .AddAw(MultipleChoiceAw, question2)
                            .AddEv("aliasKeyDn", delegate(jQueryEvent e) { if (e.Which == 13) Elements[contBtnName].Plugin<Jewl>().EndEvent(e).Focus().Click(); })
                            .AddEv("aliasFocus",
                                   delegate
                                   {
                                       Elements[contBtnName].FadeIn();
                                       if (Elements[q1Name].Is(":visible"))
                                           Truples[q1Name].RewindTo(0);
                                       if (Elements[q2Name].Is(":visible"))
                                           Truples[q2Name].RewindTo(0);
                                   })
                            .AddDx(
                                delegate(Await aw)
                                {
                                    Truples[aliasName].InputElement()
                                        .Keydown(aw.Events["aliasKeyDn"])
                                        .FocusIn(aw.Events["aliasFocus"])
                                        ;

                                    Elements[q1Name].Find("input[type=radio]")
                                        .Click(delegate { AnimateInAw(Await.AsyncAw, q2Name); })
                                        ;

                                    jQuery.Select("input[type=radio]", Elements[q2Name])
                                        .Bind("click", delegate { Master.GoPage(nextPage); })
                                        ;

                                    Truples[aliasName].FollowFocus();
                                })
                            .AddAw(AnimateInAw, aliasName)
                            .AddEv("contClick",
                                   delegate(jQueryEvent e)
                                   {
                                       if (PlayAliasValidate(e, true))
                                       {
                                           Elements[contBtnName].Hide();
                                           AnimateInAw(Await.AsyncAw, q1Name);
                                           string alias = Strings.ParaCase(Truples[aliasName].InputElement().Plugin<Jewl>().GetFValue().ToString());
                                           if (!String.IsNullOrEmpty(alias))
                                           {
                                               Master.Profile.MyAlias = alias;
                                               Truples[aliasName].InputElement().Plugin<Jewl>().FValue(Master.Profile.MyAlias);
                                               Master.Profile.Saved(false);
                                               Master.Profile.SaveMyProfileAsync(Await.AsyncAw, PkgBase.ResultRx);
                                           }
                                           RedrawAw(Await.AsyncAw, Truples[q1Name]);
                                           ((YoshiPage)Master.Page).ShowTopUser();
                                       }
                                   }
                            )
                            .AddDx(
                                delegate(Await aw)
                                {
                                    Elements[contBtnName] = jQuery.FromHtml("<button class='ContinueBtn'/>")
                                        .AppendTo(Elements[aliasName])
                                        .Html(NewBeeTheme.ContinueBtn)
                                        .Click(aw.Events["contClick"])
                                        .Hide()
                                        .FadeIn()
                                        ;

                                    Truples[q1Name].RecordFrame();
                                    Truples[q2Name].RecordFrame();
                                })
                            .AddDx(delegate { Truples[aliasName].InputElement().Focus(); })
                            .Commit();
                    })
                .Commit();

        }

    }
}